%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SVM Decoding
%
% GE_Step1_classifierapply.m
%
% Modified from Dimitrios Pantazis's code 
%  by SA 2018-03-30, 2018-04-11
%
% Adapted to the "32sclices" analysis 
%  by SA 2019-08-13
%    - removed all plotting to be able to run in a terminal..
%% Modified to allow for multiple condition breakdowns
%  by DS 2021-03
%	 - cleaned up plotting variables
%
% Modified to run subjects in parallel
%  by DS 2021-04-08
%

%initialize

clear all;
close all;

svm_dir = '/autofs/space/clive_001/users/adriana/GE_SVM';
scripts_dir = sprintf('%s/scripts', svm_dir);
functions_dir = sprintf('%s/scripts/Functions', svm_dir);

%cd(svm_dir);

addpath(genpath(scripts_dir));
addpath(genpath(functions_dir));

%SubjectNames = {'GE_15', 'GE_16', 'GE_17', 'GE_18', 'GE_19', 'GE_20', 'GE_21', 'GE_22', 'GE_23', 'GE_24'}; %{'GE_05', 'GE_06', 'GE_07', 'GE_08', 'GE_09', 'GE_10', 'GE_11', 'GE_12', 'GE_13', 'GE_14', }; % 
%SubjectNames = {'GE_07'};
SubjectNames = {'GE_15', 'GE_18', 'GE_23', 'GE_24'};
n_subjects = length(SubjectNames);

%analysis_tag = '_neighbors_vs_seeds_32slices'
analysis_tag = '';

condition_tag = 'pos' % 'nvs'|'lvn'|'pos'|'sop'|'nvl'|'svn'

% Specify the Regions of Interest ("ROIs", also called "labels":
%roiset_array = {'pMTG'};
%roiset_array = {'LIFG', 'SMG'};
%roiset_array = {'SMG', 'pMTG'};
% roiset_array = {'newSMG', 'newpMTG', 'TempPole', 'FrontalPole', 'CentGyri'};
%roiset_array = {'L_MTG1', 'L_MTG2', 'R_MTG1', 'R_MTG2'}
roiset_array = {'R_ITG_3-rh', 'R_LOC_1-rh', 'R_MTG_1-rh', 'R_MTG_2-rh', 'R_MTG_3-rh', 'R_MTG_4-rh', 'R_postCG_3-rh', 'R_postCG_4-rh', 'R_SFG_2-rh', 'R_STG_1-rh', 'R_STG_3-rh', 'L_SFG_1-lh', 'R_postCG_1-rh', 'R_postCG_5-rh', 'R_STG_2-rh', 'R_SFG_3-rh'}; %{'L_CC_1-lh', 'L_cMFG_1-lh', 'L_ParaHip_1-lh', 'L_ParsTri_1-lh', 'L_postCG_1-lh', 'R_ParsOrb_1-rh', 'R_postCG_2-rh', 'R_preCG_1-rh', 'R_preCG_2-rh', 'R_SFG_1-rh', 'L_ITG_1-lh', 'L_ITG_2-lh', 'L_MTG_1-lh', 'L_MTG_2-lh', 'L_ParsOrb_1-lh', 'L_SMG_1-lh', 'L_SPC_1-lh', 'L_STG_2-lh', 'L_STG_1-lh', 'L_TPol_1-lh', 'R_AG_1-rh', 'R_ITG_1-rh', 'R_ITG_2-rh', 
n_roisets = length(roiset_array);

% Specify the condition pairs:
switch condition_tag
	case 'nvs'
		%Train all neighbors; test hub words
		train_conditionA_array = {'11','11','11','11','11','22','22','22','22','33','33','33','44','44','55'};
		train_conditionB_array = {'22','33','44','55','66','33','44','55','66','44','55','66','55','66','66'};
		test_conditionA_array  = { '1', '1', '1', '1', '1', '2', '2', '2', '2', '3', '3', '3', '4', '4', '5'};
		test_conditionB_array  = { '2', '3', '4', '5', '6', '3', '4', '5', '6', '4', '5', '6', '5', '6', '6'};
	case 'lvn'
		%Separately train lexical and nonword neighbors; test hub words
		train_conditionA_array = {'10','10','10','10','10','20','20','20','20','30','30','30','40','40','50'};							%{'100','100','100','100','100','200','200','200','200','300','300','300','400','400','500',... 
		
		train_conditionB_array = {'20','30','40','50','60','30','40','50','60','40','50','60','50','60','60'};%{'200','300','400','500','600','300','400','500','600','400','500','600','500','600','600',...
		
		test_conditionA_array = {'1','1','1','1','1','2','2','2','2','3','3','3','4','4','5'};%,...
%									'1','1','1','1','1','2','2','2','2','3','3','3','4','4','5'};
		test_conditionB_array = {'2','3','4','5','6','3','4','5','6','4','5','6','5','6','6'};%,...
%									'2','3','4','5','6','3','4','5','6','4','5','6','5','6','6'};
	case 'pos'
		%Separately train based on differential position; test hub words
		train_conditionA_array = {'5001'}; %{'1003', '1003', '1003', '1003', '1003', '2003', '2003', '2003', '2003', '3003', '3003', '3003', '4003', '4003', '5003'};
%{'1001', '1001', '1001', '1001', '1001', '2001', '2001', '2001', '2001', '3001', '3001', '3001', '4001', '4001', '5001',...
							
		train_conditionB_array = {'6001'}; %{'2003', '3003', '4003', '5003', '6003', '3003', '4003', '5003', '6003', '4003', '5003', '6003', '5003', '6003', '6003'};%{'2001', '3001', '4001', '5001', '6001', '3001', '4001', '5001', '6001', '4001', '5001', '6001', '5001', '6001', '6001',...
									{'2003', '3003', '4003', '5003', '6003', '3003', '4003', '5003', '6003', '4003', '5003', '6003', '5003', '6003', '6003'};
		test_conditionA_array = {'5'}; % {'1','1','1','1','1','2','2','2','2','3','3','3','4','4','5'};%{'1','1','1','1','1','2','2','2','2','3','3','3','4','4','5',...
									
		test_conditionB_array = {'6'}; % {'2','3','4','5','6','3','4','5','6','4','5','6','5','6','6'};%{'2','3','4','5','6','3','4','5','6','4','5','6','5','6','6',...
									
    
    case 'sop'	
    %Train on hub words; test separately based on change position
        test_conditionA_array = {'1001', '1001', '1001', '1001', '1001', '2001', '2001', '2001', '2001', '3001', '3001', '3001', '4001', '4001', '5001',...
									'1002', '1002', '1002', '1002', '1002', '2002', '2002', '2002', '2002', '3002', '3002', '3002', '4002', '4002', '5002',...
									'1003', '1003', '1003', '1003', '1003', '2003', '2003', '2003', '2003', '3003', '3003', '3003', '4003', '4003', '5003'};
		test_conditionB_array = {'2001', '3001', '4001', '5001', '6001', '3001', '4001', '5001', '6001', '4001', '5001', '6001', '5001', '6001', '6001',...
									'2002', '3002', '4002', '5002', '6002', '3002', '4002', '5002', '6002', '4002', '5002', '6002', '5002', '6002', '6002',...
									'2003', '3003', '4003', '5003', '6003', '3003', '4003', '5003', '6003', '4003', '5003', '6003', '5003', '6003', '6003'};
		train_conditionA_array = {'1','1','1','1','1','2','2','2','2','3','3','3','4','4','5',...
									'1','1','1','1','1','2','2','2','2','3','3','3','4','4','5',...
									'1','1','1','1','1','2','2','2','2','3','3','3','4','4','5'};
		train_conditionB_array = {'2','3','4','5','6','3','4','5','6','4','5','6','5','6','6',...
									'2','3','4','5','6','3','4','5','6','4','5','6','5','6','6',...
									'2','3','4','5','6','3','4','5','6','4','5','6','5','6','6'};
		condition_tag = 'pos';
    case 'nvl'
		%Train on hub words; test separately on words and nonword neighbors (random subset)
		test_conditionA_array = {'100','100','100','100','100','200','200','200','200','300','300','300','400','400','500',...
									'10','10','10','10','10','20','20','20','20','30','30','30','40','40','50'};
		test_conditionB_array = {'200','300','400','500','600','300','400','500','600','400','500','600','500','600','600',...
									'20','30','40','50','60','30','40','50','60','40','50','60','50','60','60'};
		train_conditionA_array = {'1','1','1','1','1','2','2','2','2','3','3','3','4','4','5',...
									'1','1','1','1','1','2','2','2','2','3','3','3','4','4','5'};
		train_conditionB_array = {'2','3','4','5','6','3','4','5','6','4','5','6','5','6','6',...
									'2','3','4','5','6','3','4','5','6','4','5','6','5','6','6'};
    case 'svn'
		%Train on hub words; test on all neighbors (random subset)    
		test_conditionA_array = {'11', '11', '11', '11', '11', '22', '22', '22', '22', '33', '33', '33', '44', '44', '55'}
		test_conditionB_array = {'22', '33', '44', '55', '66', '33', '44', '55', '66', '44', '55', '66', '55', '66', '66'};
		train_conditionA_array = {'1', '1', '1', '1', '1', '2', '2', '2', '2', '3', '3', '3', '4', '4', '5'};
		train_conditionB_array = {'2', '3', '4', '5', '6', '3', '4', '5', '6', '4', '5', '6', '5', '6', '6'}; 
	case 'biphone'
		test_conditionA_array = {'1', '1', '1', '1', '1', '2', '2', '2', '2', '3', '3', '3', '4', '4', '5'...
									'1', '1', '1', '1', '1', '2', '2', '2', '2', '3', '3', '3', '4', '4', '5'};
		test_conditionB_array = {'2', '3', '4', '5', '6', '3', '4', '5', '6', '4', '5', '6', '5', '6', '6'...
									'2', '3', '4', '5', '6', '3', '4', '5', '6', '4', '5', '6', '5', '6', '6'};
%      test_conditionA_array = {'1', '1', '1', '1', '1', '2', '2', '2', '2', '3', '3', '3', '4', '4', '5'...
% 									'1', '1', '1', '1', '1', '2', '2', '2', '2', '3', '3', '3', '4', '4', '5'...
% 									'1', '1', '1', '1', '1', '2', '2', '2', '2', '3', '3', '3', '4', '4', '5'...
% 									'1', '1', '1', '1', '1', '2', '2', '2', '2', '3', '3', '3', '4', '4', '5'};
% 		test_conditionB_array = {'2', '3', '4', '5', '6', '3', '4', '5', '6', '4', '5', '6', '5', '6', '6'...
% 									'2', '3', '4', '5', '6', '3', '4', '5', '6', '4', '5', '6', '5', '6', '6'...
% 									'2', '3', '4', '5', '6', '3', '4', '5', '6', '4', '5', '6', '5', '6', '6'...
% 									'2', '3', '4', '5', '6', '3', '4', '5', '6', '4', '5', '6', '5', '6', '6'};                           
		train_conditionA_array = {'1201', '1201', '1201', '1201', '1201', '2201', '2201', '2201', '2201', '3201', '3201', '3201', '4201', '4201', '5201'...
									'1202', '1202', '1202', '1202', '1202', '2202', '2202', '2202', '2202', '3202', '3202', '3202', '4202', '4202', '5202'};...
								
		train_conditionB_array = {'2201', '3201', '4201', '5201', '6201', '3201', '4201', '5201', '6201', '4201', '5201', '6201', '5201', '6201', '6201'... 
									'2202', '3202', '4202', '5202', '6202', '3202', '4202', '5202', '6202', '4202', '5202', '6202', '5202', '6202', '6202'};
				

		%train_conditionA_array = {'1201', '1201', '1201', '1201', '1201', '2201', '2201', '2201', '2201', '3201', '3201', '3201', '4201', '4201', '5201'...
		%							'1301', '1301', '1301', '1301', '1301', '2301', '2301', '2301', '2301', '3301', '3301', '3301', '4301', '4301', '5301'...
		%							'1202', '1202', '1202', '1202', '1202', '2202', '2202', '2202', '2202', '3202', '3202', '3202', '4202', '4202', '5202'...
		%							'1302', '1302', '1302', '1302', '1302', '2302', '2302', '2302', '2302', '3302', '3302', '3302', '4302', '4302', '5302'};
		%train_conditionB_array = {'2201', '3201', '4201', '5201', '6201', '3201', '4201', '5201', '6201', '4201', '5201', '6201', '5201', '6201', '6201'... 
		%							'2301', '3301', '4301', '5301', '6301', '3301', '4301', '5301', '6301', '4301', '5301', '6301', '5301', '6301', '6301'...
		%							'2202', '3202', '4202', '5202', '6202', '3202', '4202', '5202', '6202', '4202', '5202', '6202', '5202', '6202', '6202'...
		%							'2302', '3302', '4302', '5302', '6302', '3302', '4302', '5302', '6302', '4302', '5302', '6302', '5302', '6302', '6302'};
end


n_condpairs = min(length(train_conditionA_array), length(train_conditionB_array));

plot_only = 0;   % compute and visualize

%-----------------------------------------------------------------
parpool(4);
    
parfor (i_subject = 1:length(SubjectNames), 16) %for all subjects
    %i_subject = 1;
    subject = char(SubjectNames(i_subject));
    %subject_dir=char(SubjectDirectories(i_subject));
    subject_dir = char(strcat(subject, analysis_tag));

    % Specify parameters
	param = struct('data_type', 'SOURCE', 'f_lowpass', 30, 'num_permutations', 100, 'trial_bin_size', 8, 'brainstorm_db', ['/autofs/space/clive_001/users/adriana/GE_SVM/' subject_dir]);
	%param.data_type = 'SOURCE';
	%param.f_lowpass = 30;
	%param.num_permutations = 100;
	%param.trial_bin_size = 10;
	%param.trial_bin_size = 8; %used for 3 vs. 4
    %param.brainstorm_db = sprintf('/autofs/space/clive_001/users/adriana/GE_SVM/%s',subject_dir); %directory of 'data' folder
    
    for i_condpair = 1:n_condpairs
    
        for i_roiset = 1:n_roisets

            roiset = char(roiset_array(i_roiset));

            train_conditionA= char(train_conditionA_array(i_condpair));
            train_conditionB= char(train_conditionB_array(i_condpair));
            test_conditionA= char(test_conditionA_array(i_condpair));
            test_conditionB= char(test_conditionB_array(i_condpair));

            %Run SVM analysis

            epochfile = sprintf('%s/%s/epochs/%s_%s_%s_all.mat', svm_dir, subject_dir, subject, roiset, condition_tag);
            
            
            %[accuracy,Time] = svm_contrast_conditions_perm(SubjectNames{i},condA,condB,param);
            %[accuracy,Time] = GE_svm_contrast_conditions_perm(epochfile,condA,condB,param);
            [accuracy,Time] = GE_svm_contrast_conditions_perm_diftest(epochfile,train_conditionA,train_conditionB,test_conditionA,test_conditionB,param);
            
            

            resultsdir = sprintf('%s/%s/results', svm_dir, subject_dir);
            mkdir(resultsdir);
            resultsfile = sprintf('%s/%s/results/%s_%s_train%sand%s_test%svs%s_Accuracy.mat', svm_dir, subject_dir, subject, roiset,train_conditionA,train_conditionB,test_conditionA,test_conditionB)

            
            %save(resultsfile,'Accuracy','Time','param');
            parsave(resultsfile, accuracy, Time, param); % save each subject's results separately
            

        end % i_roiset
    
    end % i_condition

end % i_subject


function parsave(fname, x,y,z)
	save(fname, 'x', 'y', 'z')
end



